﻿namespace Pub.Class
{
    using System;
    using System.Web;
    using System.Web.UI.HtmlControls;

    public class Msg
    {
        public static void CompleteRequest()
        {
            HttpContext.Current.ApplicationInstance.CompleteRequest();
        }

        public static void End()
        {
            HttpContext.Current.Response.End();
        }

        public static void Flush()
        {
            HttpContext.Current.Response.Flush();
        }

        public static void NoCache()
        {
            HttpContext.Current.Response.BufferOutput = false;
            HttpContext.Current.Response.ExpiresAbsolute = DateTime.Now.AddDays(-1.0);
            HttpContext.Current.Response.Cache.SetExpires(DateTime.Now.AddDays(-1.0));
            HttpContext.Current.Response.Expires = 0;
            HttpContext.Current.Response.CacheControl = "no-cache";
            HttpContext.Current.Response.Cache.SetNoStore();
        }

        public static void Redirect(string url)
        {
            HttpContext.Current.Response.Redirect(url);
        }

        public static void Transfer(string url)
        {
            HttpContext.Current.Server.Transfer(url);
        }

        public static void Write(object msg)
        {
            Write(msg.ToString());
        }

        public static void Write(string msg)
        {
            HttpContext.Current.Response.Write(msg);
        }

        public static void Write(string format, params object[] args)
        {
            HttpContext.Current.Response.Output.Write(format, args);
        }

        public static void Write(HtmlGenericControl Control, string msg, bool isAdd)
        {
            if (isAdd)
            {
                Control.InnerHtml = Control.InnerHtml + msg;
            }
            else
            {
                Control.InnerHtml = msg;
            }
        }

        public static void WriteEnd(object msg)
        {
            WriteEnd(msg.ToString());
        }

        public static void WriteEnd(string msg)
        {
            Write(msg);
            End();
        }

        public static void WriteEnd(string format, params object[] args)
        {
            Write(format, args);
            End();
        }

        public static void WriteHTMLEnd(string html)
        {
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ContentType = "text/html";
            HttpContext.Current.Response.Expires = 0;
            HttpContext.Current.Response.Cache.SetNoStore();
            HttpContext.Current.Response.Write(html);
            HttpContext.Current.Response.End();
        }

        public static void WriteJSONEnd(string json)
        {
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ContentType = "application/json";
            HttpContext.Current.Response.Expires = 0;
            HttpContext.Current.Response.Cache.SetNoStore();
            HttpContext.Current.Response.Write(json);
            HttpContext.Current.Response.End();
        }

        public static void WriteJSONPEnd(string json)
        {
            string q = Request2.GetQ("callback");
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ContentType = "application/javascript";
            HttpContext.Current.Response.Expires = 0;
            HttpContext.Current.Response.Cache.SetNoStore();
            HttpContext.Current.Response.Write("{0}({1});".FormatWith(q, json));
            HttpContext.Current.Response.End();
        }

        public static void WriteLine(object msg)
        {
            WriteLine(msg.ToString());
        }

        public static void WriteLine(string msg)
        {
            HttpContext.Current.Response.Write(msg + "<br />");
        }

        public static void WriteLine(string format, params object[] args)
        {
            HttpContext.Current.Response.Output.Write(format + "<br />", args);
        }

        public static void WriteTextEnd(string text)
        {
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Write(text);
            HttpContext.Current.Response.End();
        }

        public static void WriteXMLEnd(string xmlnode)
        {
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ContentType = "text/xml";
            HttpContext.Current.Response.Expires = 0;
            HttpContext.Current.Response.Cache.SetNoStore();
            HttpContext.Current.Response.Write(xmlnode.ToString());
            HttpContext.Current.Response.End();
        }
    }
}

